package com.ruoyi.blog.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.blog.domain.BlogArticle;
import com.ruoyi.blog.domain.BlogCategory;
import com.ruoyi.blog.domain.vo.BlogArticleVo;
import com.ruoyi.blog.service.IBlogArticleService;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.blog.domain.bo.BlogCommentBo;
import com.ruoyi.blog.domain.vo.BlogCommentVo;
import com.ruoyi.blog.domain.BlogComment;
import com.ruoyi.blog.mapper.BlogCommentMapper;
import com.ruoyi.blog.service.IBlogCommentService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 博客评论Service业务层处理
 *
 * @author ruoyi
 * @date 2023-10-03
 */
@RequiredArgsConstructor
@Service
public class BlogCommentServiceImpl extends ServiceImpl<BlogCommentMapper, BlogComment> implements IBlogCommentService {

    private final BlogCommentMapper baseMapper;
    private final IBlogArticleService blogArticleService;

    /**
     * 查询博客评论
     */
    @Override
    public BlogCommentVo queryById(Long commentId){
        return baseMapper.selectVoById(commentId);
    }


    /**
     * 查询博客评论列表
     */
    @Override
    public List<BlogCommentVo> queryList(BlogCommentBo bo) {
        LambdaQueryWrapper<BlogComment> lqw = buildQueryWrapper(bo);
        List<BlogCommentVo> blogCommentVos = baseMapper.selectVoList(lqw);
        setArticleName(blogCommentVos);
        return blogCommentVos;
    }

    /**
     * 设置博客文章对应的分类
     */
    public void setArticleName(List<BlogCommentVo> list) {

        Set<String> articleIds = list.stream().map(n -> n.getArticleId()).collect(Collectors.toSet());

        if (articleIds.size() == 0){
            return;
        }

        Map<String, BlogArticle> articleMap = CollStreamUtil.toIdentityMap(blogArticleService.lambdaQuery()
            .select(BlogArticle::getTitle, BlogArticle::getArticleId)
            .in(BlogArticle::getArticleId, articleIds)
            .list(), BlogArticle::getArticleId);

        articleMap.put("0", BlogArticle.builder().title("所有").build());

        list.forEach(n -> {
            n.setArticleName(articleMap.get(n.getArticleId()).getTitle());
        });

    }

    private LambdaQueryWrapper<BlogComment> buildQueryWrapper(BlogCommentBo bo) {

        List<Long> commentIds = null;

        if (bo.getCommentId() != null){
            commentIds = baseMapper.getAllIdByParent(bo.getCommentId());
        }

        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BlogComment> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getArticleId()), BlogComment::getArticleId, bo.getArticleId());
        lqw.like(StringUtils.isNotBlank(bo.getCommentContext()), BlogComment::getCommentContext, bo.getCommentContext());
        lqw.eq(bo.getTopping() != null, BlogComment::getTopping, bo.getTopping());
        lqw.eq(bo.getIsShow() != null, BlogComment::getIsShow, bo.getIsShow());
        lqw.in(commentIds != null, BlogComment::getCommentId, commentIds);
        lqw.orderByAsc(BlogComment::getCreateTime);
        return lqw;
    }

    /**
     * 新增博客评论
     */
    @Override
    public Boolean insertByBo(BlogCommentBo bo) {
        BlogComment add = BeanUtil.toBean(bo, BlogComment.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setCommentId(add.getCommentId());
        }
        return flag;
    }

    /**
     * 修改博客评论
     */
    @Override
    public Boolean updateByBo(BlogCommentBo bo) {
        BlogComment update = BeanUtil.toBean(bo, BlogComment.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BlogComment entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除博客评论
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
